﻿using Microsoft.Data.SqlClient;

namespace Generic.Helper.Sql
{
    public static class SqlHelper
    {
        public static List<dynamic> GetDataReader(string connectionString, string commandText, List<SqlParameter> paras, Func<SqlDataReader, List<dynamic>> dataReaderDelegate)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                if (connection.State == System.Data.ConnectionState.Closed)
                    connection.Open();

                SqlCommand cmd = new SqlCommand(commandText, connection);
                paras.ForEach(item =>
                {
                    cmd.Parameters.Add(item);
                });

                return dataReaderDelegate.Invoke(cmd.ExecuteReader());
            }
        }
    }
}
